<?php

namespace app\admin\controller;

use app\helper\Helper;
use app\model\UserModel;
use think\Db;
use think\Request;
use think\Validate;

class User extends Base
{
    /**
     * 用户数据列表页面
     * @return \think\response\View
     */
    public function userIndex()
    {
        return view();
    }

    /**
     * 获取用户列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserList()
    {
        $param = $this->request->param();
        $where = 'delete_ts = 0';
        if (!empty($param['user_name'])) {
            $where .= ' and user_name like "%' . $param['user_name'] .'%"';
        }
        if (!empty($param['mobile'])) {
            $where .= ' and mobile like "%' . $param['mobile'] .'%"';
        }
        $count = Db::name('users')->where($where)->count();
        $limit = Request::instance()->param('limit')?Request::instance()->param('limit'):10;
        $page = Request::instance()->param('page')?Request::instance()->param('page'):1;
        $tol = ($page - 1) * $limit;
        $list = Db::name('users')
            ->where($where)
            ->limit($tol,$limit)
            ->order('id desc')
            ->select();
        foreach ($list as $key => $value){
            $list[$key]['add_time'] = date('Y-m-d H:i:s',$value['add_time']);
            $list[$key]['update_time'] = date('Y-m-d H:i:s',$value['update_time']);
        }
        //返回数据
        return ["code" => "0", "msg" => "", "count" => $count, "data" => $list];
    }

    public function userInfo()
    {
        $param = $this->request->param();
        if(!empty($param['id'])){
            $info = Db::table('users')->where('id',$param['id'])->find();
            $this->assign('info',$info);
        }
        return view();
    }

    public function ApiSaveUserInfo(){
        $param = $this->request->param();
        $rule = [
            'user_name|名称'=>'require|max:20',
            'email|登录账号'=>'require',
            'mobile|手机号'=>'require|/^[1][3,4,5,6,,7,8,9][0-9]{9}$/',
            'password|密码'=>'require',
            'status|状态'=>'require',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            return json(['code'=>400,'msg'=>$validate->getError()]);
        }

        $res = Db::table('users')->where('delete_ts',0)->where('mobile',$param['mobile'])->find();
        if(!empty($param['id'])){
            if(!empty($res)){
                if($res['id'] !=$param['id']){
                    return json(['code'=>400,'msg'=>'手机号已存在']);
                }
            }
        }else{
            if($res){
                return json(['code'=>400,'msg'=>'手机号已存在']);
            }
        }

        $AdminModel = new UserModel();
        $data=[
            'user_name'=> $param['user_name'],
            'email'=> $param['email'],
            'mobile'=> $param['mobile'],
            'status'=> $param['status'],
        ];
        if(!empty($param['id'])){
            $info = Db::name('users')->where('delete_ts',0)->field('password')->find($param['id']);
            if($info['password'] != $param['password']){
                $password = md5($param['password']);
                $data['password'] = $password;
            }
            $data['update_time'] = time();
            $res = $AdminModel->where('id',$param['id'])->update($data);
        }else{
            $data['password'] = md5($param['password']);
            $data['add_time'] = time();
            $res = $AdminModel->insert($data);
        }
        if($res!==false){
            return json(['code'=>200,'msg'=>'更新成功','data'=>$res]);
        }else{
            return json(['code'=>400,'msg'=>'error','data'=>$res]);
        }
    }

    public function comment()
    {
        return view();
    }

    public function getComment()
    {
        $param = $this->request->param();
        $where = [
            'delete_ts'=>0
        ];
        if (!empty($param['name'])) {
            $where['name'] = ['like','%' . $param['name'] .'%'];
        }
        $count = Db::name('user_comment')->count();

        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;

        $tol = ($page - 1) * $pageSize;
        $list = Db::name('user_comment')
            ->limit($tol,$pageSize)
            ->select();

        foreach ($list as $key => $value){
            $list[$key]['created_ts'] = date('Y-m-d H:i:s',$value['created_ts']);
            $user = Db::table('users')->where('id',$value['user_id'])->where($where)->find();
            if(!empty($user)){
                $list[$key]['user_name'] = $user['user_name'];
            }else{
                $list[$key]['user_name'] = '游客';
            }
        }

        return json(["code" => "0", "msg" => "", "count" => $count, "data" => $list]);
    }
}